<template>
	<div class="content">
		<div class="content-header">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>Demo</el-breadcrumb-item>
				<el-breadcrumb-item>
					<b>商品管理</b>
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="content-body">
			<el-scrollbar>
				<div class="panel">
					<el-form :model="goodsSearchForm" ref="goodsSearchForm" inline label-width="100px">
						<el-row>
							<el-col :md="8">
								<el-form-item label="商品类别">
									<el-select v-model="goodsSearchForm.categoryId" placeholder="全部">
										<el-option v-for="item in categoryList" :key="item.id" :label="item.categoryName" :value="item.id" ></el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :md="8">
								<el-form-item label="商品名称">
									<el-input v-model="goodsSearchForm.goodsName"></el-input>
								</el-form-item>
							</el-col>
							<el-col :md="8">
								<el-form-item label="商品标签">
									<el-input v-model="goodsSearchForm.tag"></el-input>
								</el-form-item>
							</el-col>
						</el-row>
						<div class="search-btn">
							<div class="search-btn-left">
								<el-button type="success" icon="el-icon-plus" @click="handleAdd">新增</el-button>
							</div>
							<div class="search-btn-right">
								<el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
								<el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
							</div>
						</div>
					</el-form>
					<el-table :data="tableData" border stripe style="width: 100%">
						<el-table-column label="序号" width="60" header-align="left" align="center">
							<template slot-scope="scope">{{scope.$index + 1 + (tableCurrentPage-1)*tablePageSize}}</template>
						</el-table-column>
						<el-table-column label="商品类别" prop="categoryName"></el-table-column>
						<el-table-column label="商品名称" prop="goodsName"></el-table-column>
						<el-table-column label="商品标签" prop="tag">
							
						</el-table-column>
						<el-table-column label="商品缩略图" prop="thumbImage">
							<template slot-scope="scope" >
								<el-image style="width: 60px; height: 60px;" :src="$util.showFile(scope.row.thumbImage)" fit="contain"></el-image>
							</template>
						</el-table-column>
						<el-table-column label="库存" prop="remainCount"></el-table-column>
						<el-table-column label="销量" prop="sellCount"></el-table-column>
						<el-table-column label="价格" prop="price">
							<template slot-scope="scope">
								<span>{{$util.moneyFormat(scope.row.price)}}</span>
							</template>
						</el-table-column>
						<el-table-column label="状态" prop="status">
							<template slot-scope="scope" >
								<span v-if="scope.row.status == '1'">待上架</span>
								<span v-if="scope.row.status == '2'">上架销售</span>
								<span v-if="scope.row.status == '3'">已下架</span>
							</template>
						</el-table-column>
						<el-table-column label="上架时间" prop="addTime"></el-table-column>
						<el-table-column label="操作" min-width="150px">
							<template slot-scope="scope">
								<el-button type="warning" @click="handleUpdate(scope.row.id)">编辑</el-button>
								<el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page.sync="tableCurrentPage" :page-sizes="[10, 20, 30, 50]"
						:page-size.sync="tablePageSize" layout="total, jumper, prev, pager, next, sizes"
						:total="tableTotal"></el-pagination>
				</div>
			</el-scrollbar>
			<Goods ref="goods"></Goods>
		</div>
	</div>
</template>

<script>
import Goods from "./Goods";
export default {
	components: {
		Goods
	},
	data() {
		return {
			goodsSearchForm: {
				categoryId: null,
				goodsName: null,
				tag: null
			},
			categoryList: [],
			tableTotal: 0,
			tableCurrentPage: 1,
			tablePageSize: 10,
			tableData: []
		};
	},
	mounted() {
		this.$api.post('/goodsCategory/findAll').then(
			response => {
				this.categoryList = response.data;
			}
		);
		this.getTableData(this.tableCurrentPage, this.tablePageSize);
	},
	methods: {
		handleSizeChange(val) {
			this.getTableData(this.tableCurrentPage, val);
		},
		handleCurrentChange(val) {
			this.getTableData(val, this.tablePageSize);
		},
		handleSearch() {
			this.getTableData(this.tableCurrentPage, this.tablePageSize);
		},
		getTableData(page, pageSize) {
			
			let pageParams = '?page=' + page + '&rows=' + pageSize;

			this.$api.post('/goods/findByPage' + pageParams, this.goodsSearchForm).then(
				response => {
					this.tableData = response.data.list;
					this.tableTotal = response.data.total;
				}
			);
		},
		resetForm() {
			this.goodsSearchForm = {
				categoryId: null,
				goodsName: null,
				tag: null
			};
			this.getTableData(1, this.tablePageSize);
		},
		handleAdd() {
			this.$refs.goods.initForm();
		},
		handleUpdate(id) {
			this.$refs.goods.initForm(id);
		},
		handleDelete(id) {
			this.$confirm("确认删除?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(() => {
				this.$api.post('/goods/delete', {id:id}).then(response => {
					if (response.success) {
						this.$message.success(response.message);
						this.handleSearch();
					} else {
						this.$message.error(response.message);
					}
				});
			}).catch(() => {});
		}
	}
};
</script>